package net.kts.attraction.subway.smallpass.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.util.Log;

import net.kts.attraction.subway.smallpass.model.LastPath;
import net.kts.attraction.subway.smallpass.model.PassLine;
import net.kts.attraction.subway.smallpass.model.PassStation;
import net.kts.attraction.subway.smallpass.model.infomation.MultiLineStation;
import net.kts.attraction.subway.smallpass.model.infomation.MultiLineTime;
import net.kts.attraction.subway.smallpass.model.infomation.StationInfo;
import net.kts.attraction.subway.smallpass.model.infomation.StationTransfer;
import net.kts.attraction.subway.smallpass.model.infomation.StationTransferTime;
import net.kts.attraction.subway.smallpass.model.infomation.TransferInfo;
import net.kts.attraction.subway.smallpass.process.check.SmallWayChecker;

public class SmallPassSubway {

	// ����ö ������ �ҿ�ð�     // key, value = ������ ���ڵ� , ������ �ҿ�ð� model
	private Map<Integer, List<MultiLineTime>> multiLineTimeMap = new HashMap<Integer, List<MultiLineTime>>();
	// ������ ����    // key, value= ������ ���ڵ� , ������ ���� model
	private Map<Integer, MultiLineStation> multiLineStationMap = new HashMap<Integer, MultiLineStation>();

	// ȯ�¿� ������
	private Map<Integer, StationTransfer> stationTransferMap = new HashMap<Integer, StationTransfer>();

	// ȯ�� �ð� ����  key, value = stationCd, ȯ�¿� �ҿ�ð�����
	private Map<Integer, StationTransferTime> stationTransferTimeMap = new HashMap<Integer, StationTransferTime>();

	// ������  key, value = ���ڵ� , ������
	private Map<Integer, StationInfo> stationInfoMap = new HashMap<Integer, StationInfo>();

	//���� ȯ��, ������ ���� Ŭ����
	private SmallWayPass smallWayPass = null;

	// ó������ ȯ�¿��϶�! ȯ�¿����� �������� ó���ߴ��� ���ߴ��� Ȯ�ο�
	private boolean transferMultiLineFlag = false;

	public SmallPassSubway(Map<Integer, List<MultiLineTime>> multiLineTimeMap, Map<Integer, MultiLineStation> multiLineStationMap,
			Map<Integer, StationTransfer> stationTransferMap, Map<Integer, StationTransferTime> stationTransferTimeMap,
			Map<Integer, StationInfo> stationInfoMap) {

		this.multiLineTimeMap = multiLineTimeMap;
		this.multiLineStationMap = multiLineStationMap;
		this.stationTransferMap = stationTransferMap;
		this.stationTransferTimeMap = stationTransferTimeMap;
		this.stationInfoMap = stationInfoMap;

		this.smallWayPass = new SmallWayPass(this.stationTransferMap, this.stationInfoMap, this.multiLineStationMap);	
	}


	public void goSmallWay(int start, int target) {
		// ���ۿ� ���� ���� ����
		StationInfo startStation = this.stationInfoMap.get(start);
		StationInfo targetStation = this.stationInfoMap.get(target);

		LastPath lastPath = new LastPath();
		lastPath.setStartStation(startStation);
		lastPath.setEndStation(targetStation);

		if(SmallWayChecker.stationLineChecker.isLineMulti(targetStation.getStationCode())) {
			StationTransfer stationTransfer = this.stationTransferMap.get(targetStation.getStationCode());
			lastPath.setEndList(stationTransfer.getTransferInfoList());

		}


		// ���Ŀ� �������� ���� ���ۿ��� �����Ѵ�.
		List<PassStation> passStationList = new ArrayList<PassStation>();

		// ���Ŀ� ȣ�������� ���Ѵ�.
		List<PassLine> passLineList = new ArrayList<PassLine>();

		//������ ȣ�������� ����
		passLineList.add(this.smallWayPass.settingPassLine(startStation, startStation.getLineNum(), startStation.getStationCode(), 0, 1)); // 1 ���
		// ������ �뼱������ ����!
		passStationList.add(this.smallWayPass.settingPassStation(startStation, 0));

		lastPath.setPassLineList(passLineList);
		lastPath.setPassStationList(passStationList);

		Set<Integer> stationSet = new HashSet<Integer>();
		stationSet.add(startStation.getStationCode());

		lastPath.setStationSet(stationSet);

		// ���� �˻�!


		Log.d("kts", "before if SmallWayChecker.stationLineChecker.isLineMulti(startStation.getStationCode()");
		// ȯ�¿��� �����ϴ°�!
		if(SmallWayChecker.stationLineChecker.isLineMulti(startStation.getStationCode())) {

			// ��߿��� ȯ�¿��϶��� ;; ����� ������ �߰��� �������� �����.
//			lastPath.getPassStationList().remove(0);

			Log.d("kts", "if SmallWayChecker.stationLineChecker.isLineMulti(startStation.getStationCode()");

			StationTransfer stationTransfer = stationTransferMap.get(startStation.getStationCode());

			for(TransferInfo transferInfo : stationTransfer.getTransferInfoList()) {

				StationInfo stationInfo = this.stationInfoMap.get(transferInfo.getStationCode());

				if(stationInfo == null) {
					continue;
				}

				//������ ȣ�������� ����
				//��! ����Ҷ� ���������� ��߿��� ���� ȣ�������� �н�
				if(startStation.getStationCode() != transferInfo.getStationCode()) {
					lastPath.getPassLineList().add(this.smallWayPass.settingPassLine(startStation, transferInfo.getLinenum(), transferInfo.getStationCode(), 0, 3)); // 3 ȯ��
				}
				
				// ������ �뼱������ ����!
//				lastPath.getPassStationList().add(this.smallWayPass.settingPassStation(stationInfo, 0));

//				lastPath.getStationSet().add(stationInfo.getStationCode());

				// ���� �뼱�� �������̶�?
//				if(SmallWayChecker.multiLineChecker.isMultiLine(stationInfo.getStationCode())) {
//					this.transferMultiLineFlag = true;
//					multiLineProcess(stationInfo, lastPath);
//
//				}
				// �ƴϸ� �׳� ���
//				else {
					this.smallWayPass.threadStart(stationInfo, lastPath);

//				}		 


			}

		}



		// ȯ�¿��� �ƴ϶��?
		else {

			Log.d("kts", "else SmallWayChecker.stationLineChecker.isLineMulti(startStation.getStationCode()");

			if(!this.transferMultiLineFlag ) {
				//�������ΰ�?
				if( SmallWayChecker.multiLineChecker.isMultiLine(startStation.getStationCode())) {

					multiLineProcess(startStation, lastPath);
				}
				//�ƴϳ� �׳� ����!
				else {
					//�������� �������� ���!
					this.smallWayPass.threadStart(startStation,lastPath);

				}
			}

		}
	}
	// ���� ȣ�� ���� ��ü ��
//	public PassLine settingPassLine(StationInfo stationInfo, double time, int type) {
//
//		PassLine passLine = new PassLine();
//
//		passLine.setLineNum(stationInfo.getLineNum());
//		passLine.setTime(time);
//		passLine.setType(type);
//
//		return passLine;
//
//	}


	// ���Ŀ� �뼱 ���� ��ü ���ּ�
//	public PassStation settingPassStation(StationInfo stationInfo , double time) {
//
//		PassStation passStation = new PassStation();
//
//		passStation.setLineNum(stationInfo.getLineNum());
//		passStation.setStationCode(stationInfo.getStationCode());
//		passStation.setTime(time);
//
//		return passStation;
//	}

	// ������ �ҿ�ð� ������ - ó�� ��ŸƮ��
	public double getMultiLineTime(int startStationCode, int targetStationCode) {


		List<MultiLineTime> multiLineTimeList = this.multiLineTimeMap.get(startStationCode);

		for(MultiLineTime multiLineTime : multiLineTimeList) {

			// ���۰� ���� �ڵ尡 �����Ҷ�! �� �ش� �ð����� ����!
			if(multiLineTime.getPrevStationCode() == startStationCode && multiLineTime.getNextStationCode() == targetStationCode) {
				return multiLineTime.getMultiLineTime();
			}

		}

		return 0;

	}



	public void multiLineProcess(StationInfo currentStationInfo, LastPath lastPath) {

		MultiLineStation multiLineStation=  this.multiLineStationMap.get(currentStationInfo.getStationCode());

		// ������ ������ ��� ��������!
		List<Integer> nextList =  multiLineStation.getNextStationCodeList();
		LastPath cloneLastPath = null;
		//			int nextStationCode = 0;
				
		StationInfo nextStationInfo = null;
		//			PassLine tempPassLine = null;
		for(int indexI = 0 ; indexI < nextList.size(); ++indexI) {

			// ���� �ٸ� �������̸� ������ ���ϸ鼭 ���۽��Ѷ�!

			nextStationInfo = this.stationInfoMap.get(nextList.get(indexI));			

			// ���� ���� ����!
			try {
				cloneLastPath = (LastPath) lastPath.clone();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// ���� �뼱������ �߰���!
			
			// ���翪
//			double time = SmallWayChecker.timeChecker.multiLineTimeCheck(currentStationInfo.getStationCode(), currentStationInfo.getStationCode(), nextStationInfo.getStationCode());
//			cloneLastPath.getPassStationList().add(this.smallWayPass.settingPassStation(nextStationInfo, time));
//			cloneLastPath.setLastTime(cloneLastPath.getLastTime() + time);
//			cloneLastPath.getStationSet().add(currentStationInfo.getStationCode());
			//����! ������Ǵ� ȯ�¿��϶�!
//			if(SmallWayChecker.multiLineChecker.isMultiLine(nextStationInfo.getStationCode())
//					|| SmallWayChecker.stationLineChecker.isLineMulti(nextStationInfo.getStationCode())) {
//				this.smallWayPass.threadSingleStart(currentStationInfo, nextStationInfo, cloneLastPath);
//				continue;
//			}


			this.smallWayPass.threadMultiLineStart(currentStationInfo, nextStationInfo, cloneLastPath);
//			try {
//				this.smallWayPass.threadStart(nextStationInfo, (LastPath)cloneLastPath.clone());
//			} catch (CloneNotSupportedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}



		}

		//		}

	}

}
